/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.spring;

import com.jakewharton.nopen.annotation.Open;

import io.sentry.Breadcrumb;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.util.Objects;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.Ordered;

/**
 * Pushes new {@link io.sentry.Scope} on each incoming HTTP request.
 */
@Open
public class SentrySpringRequestListener implements ServletRequestListener, Ordered {
    private final @NotNull IHub hub;
    private final @NotNull SentryRequestResolver requestResolver;

    /**
     * Creates a new instance of {@link SentrySpringRequestListener}. Used in traditional servlet
     * containers with {@link SentrySpringServletContainerInitializer}.
     */
    public SentrySpringRequestListener() {
        this(HubAdapter.getInstance());
    }

    /**
     * Creates a new instance of {@link SentrySpringRequestListener}. Used together with Spring Boot
     * or with embedded servlet containers.
     *
     * @param hub - the hub
     * @param requestResolver - the request resolver
     */
    public SentrySpringRequestListener(
        final @NotNull IHub hub, final @NotNull SentryRequestResolver requestResolver) {
        this.hub = Objects.requireNonNull(hub, "hub is required");
        this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver are required");
    }

    SentrySpringRequestListener(final @NotNull IHub hub) {
        this(hub, new SentryRequestResolver(hub));
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        hub.popScope();
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        hub.pushScope();

        final ServletRequest servletRequest = sre.getServletRequest();
        if (servletRequest instanceof HttpServletRequest) {
            final HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
            hub.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()));

            hub.configureScope(
                scope -> {
                    scope.setRequest(requestResolver.resolveSentryRequest(request));
                    scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(request));
                });
        }
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
